package com.baobao.dao;

import com.baobao.domain.Banji;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.skyway.spring.util.dao.AbstractJpaDao;

import org.springframework.dao.DataAccessException;

import org.springframework.stereotype.Repository;

import org.springframework.transaction.annotation.Transactional;

/**
 * DAO to manage Banji entities.
 * 
 */
@Repository("BanjiDAO")
@Transactional
public class BanjiDAOImpl extends AbstractJpaDao<Banji> implements BanjiDAO {

	/**
	 * Set of entity classes managed by this DAO.  Typically a DAO manages a single entity.
	 *
	 */
	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(Arrays.asList(new Class<?>[] { Banji.class }));

	/**
	 * EntityManager injected by Spring for persistence unit BaoBao
	 *
	 */
	@PersistenceContext(unitName = "BaoBao")
	private EntityManager entityManager;

	/**
	 * Instantiates a new BanjiDAOImpl
	 *
	 */
	public BanjiDAOImpl() {
		super();
	}

	/**
	 * Get the entity manager that manages persistence unit 
	 *
	 */
	public EntityManager getEntityManager() {
		return entityManager;
	}

	/**
	 * Returns the set of entity classes managed by this DAO.
	 *
	 */
	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	/**
	 * JPQL Query - findBanjiByMasterPhone
	 *
	 */
	@Transactional
	public Set<Banji> findBanjiByMasterPhone(String masterPhone) throws DataAccessException {

		return findBanjiByMasterPhone(masterPhone, -1, -1);
	}

	/**
	 * JPQL Query - findBanjiByMasterPhone
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Banji> findBanjiByMasterPhone(String masterPhone, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findBanjiByMasterPhone", startResult, maxRows, masterPhone);
		return new LinkedHashSet<Banji>(query.getResultList());
	}

	/**
	 * JPQL Query - findBanjiByIdContaining
	 *
	 */
	@Transactional
	public Set<Banji> findBanjiByIdContaining(String id) throws DataAccessException {

		return findBanjiByIdContaining(id, -1, -1);
	}

	/**
	 * JPQL Query - findBanjiByIdContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Banji> findBanjiByIdContaining(String id, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findBanjiByIdContaining", startResult, maxRows, id);
		return new LinkedHashSet<Banji>(query.getResultList());
	}

	/**
	 * JPQL Query - findBanjiBySchoolIdContaining
	 *
	 */
	@Transactional
	public Set<Banji> findBanjiBySchoolIdContaining(String schoolId) throws DataAccessException {

		return findBanjiBySchoolIdContaining(schoolId, -1, -1);
	}

	/**
	 * JPQL Query - findBanjiBySchoolIdContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Banji> findBanjiBySchoolIdContaining(String schoolId, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findBanjiBySchoolIdContaining", startResult, maxRows, schoolId);
		return new LinkedHashSet<Banji>(query.getResultList());
	}

	/**
	 * JPQL Query - findAllBanjis
	 *
	 */
	@Transactional
	public Set<Banji> findAllBanjis() throws DataAccessException {

		return findAllBanjis(-1, -1);
	}

	/**
	 * JPQL Query - findAllBanjis
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Banji> findAllBanjis(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllBanjis", startResult, maxRows);
		return new LinkedHashSet<Banji>(query.getResultList());
	}

	/**
	 * JPQL Query - findBanjiByPrimaryKey
	 *
	 */
	@Transactional
	public Banji findBanjiByPrimaryKey(String id) throws DataAccessException {

		return findBanjiByPrimaryKey(id, -1, -1);
	}

	/**
	 * JPQL Query - findBanjiByPrimaryKey
	 *
	 */

	@Transactional
	public Banji findBanjiByPrimaryKey(String id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findBanjiByPrimaryKey", startResult, maxRows, id);
			return (com.baobao.domain.Banji) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	/**
	 * JPQL Query - findBanjiBySchoolId
	 *
	 */
	@Transactional
	public Set<Banji> findBanjiBySchoolId(String schoolId) throws DataAccessException {

		return findBanjiBySchoolId(schoolId, -1, -1);
	}

	/**
	 * JPQL Query - findBanjiBySchoolId
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Banji> findBanjiBySchoolId(String schoolId, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findBanjiBySchoolId", startResult, maxRows, schoolId);
		return new LinkedHashSet<Banji>(query.getResultList());
	}

	/**
	 * JPQL Query - findBanjiByMasterName
	 *
	 */
	@Transactional
	public Set<Banji> findBanjiByMasterName(String masterName) throws DataAccessException {

		return findBanjiByMasterName(masterName, -1, -1);
	}

	/**
	 * JPQL Query - findBanjiByMasterName
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Banji> findBanjiByMasterName(String masterName, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findBanjiByMasterName", startResult, maxRows, masterName);
		return new LinkedHashSet<Banji>(query.getResultList());
	}

	/**
	 * JPQL Query - findBanjiByMasterNameContaining
	 *
	 */
	@Transactional
	public Set<Banji> findBanjiByMasterNameContaining(String masterName) throws DataAccessException {

		return findBanjiByMasterNameContaining(masterName, -1, -1);
	}

	/**
	 * JPQL Query - findBanjiByMasterNameContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Banji> findBanjiByMasterNameContaining(String masterName, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findBanjiByMasterNameContaining", startResult, maxRows, masterName);
		return new LinkedHashSet<Banji>(query.getResultList());
	}

	/**
	 * JPQL Query - findBanjiByName
	 *
	 */
	@Transactional
	public Set<Banji> findBanjiByName(String name) throws DataAccessException {

		return findBanjiByName(name, -1, -1);
	}

	/**
	 * JPQL Query - findBanjiByName
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Banji> findBanjiByName(String name, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findBanjiByName", startResult, maxRows, name);
		return new LinkedHashSet<Banji>(query.getResultList());
	}

	/**
	 * JPQL Query - findBanjiById
	 *
	 */
	@Transactional
	public Banji findBanjiById(String id) throws DataAccessException {

		return findBanjiById(id, -1, -1);
	}

	/**
	 * JPQL Query - findBanjiById
	 *
	 */

	@Transactional
	public Banji findBanjiById(String id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findBanjiById", startResult, maxRows, id);
			return (com.baobao.domain.Banji) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	/**
	 * JPQL Query - findBanjiByMasterPhoneContaining
	 *
	 */
	@Transactional
	public Set<Banji> findBanjiByMasterPhoneContaining(String masterPhone) throws DataAccessException {

		return findBanjiByMasterPhoneContaining(masterPhone, -1, -1);
	}

	/**
	 * JPQL Query - findBanjiByMasterPhoneContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Banji> findBanjiByMasterPhoneContaining(String masterPhone, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findBanjiByMasterPhoneContaining", startResult, maxRows, masterPhone);
		return new LinkedHashSet<Banji>(query.getResultList());
	}

	/**
	 * JPQL Query - findBanjiByNameContaining
	 *
	 */
	@Transactional
	public Set<Banji> findBanjiByNameContaining(String name) throws DataAccessException {

		return findBanjiByNameContaining(name, -1, -1);
	}

	/**
	 * JPQL Query - findBanjiByNameContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Banji> findBanjiByNameContaining(String name, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findBanjiByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<Banji>(query.getResultList());
	}

	/**
	 * Used to determine whether or not to merge the entity or persist the entity when calling Store
	 * @see store
	 * 
	 *
	 */
	public boolean canBeMerged(Banji entity) {
		return true;
	}
}
